package xyz.xiaoshengfu.bookstore.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.xiaoshengfu.bookstore.mapper.TbBookMapper;
import xyz.xiaoshengfu.bookstore.pojo.TbBook;
import xyz.xiaoshengfu.bookstore.pojo.TbBookExample;
import xyz.xiaoshengfu.bookstore.pojo.TbBookExample.Criteria;
import xyz.xiaoshengfu.bookstore.service.IBookService;

@Service
public class BookServiceImpl implements IBookService {

	@Autowired
	private TbBookMapper tbBookMapper;

	@Override
	public List<TbBook> getTbBookList() {
		TbBookExample tbBookExample = new TbBookExample();
		tbBookExample.createCriteria().andIsDeletedEqualTo(false);
		return tbBookMapper.selectByExample(tbBookExample);
	}

	@Override
	public List<TbBook> getTbBookListByName(String name) {
		TbBookExample tbBookExample = new TbBookExample();
		Criteria criteria = tbBookExample.createCriteria().andIsDeletedEqualTo(false);
		if (StringUtils.isNotBlank(name)) {
			criteria.andNameLike("%" + name + "%");
		}
		return tbBookMapper.selectByExample(tbBookExample);
	}

	@Override
	public boolean delTbBookById(Long id) {
		if (id != null) {
			TbBook tbBook = new TbBook();
			tbBook.setId(id);
			tbBook.setIsDeleted(true);
			int i = tbBookMapper.updateByPrimaryKeySelective(tbBook);
			if (i == 1) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean editTbBook(TbBook tbBook) {
		if (tbBook.getId() != null && tbBook.getAuthor() != null && tbBook.getBindingLayout() != null
				&& tbBook.getIsbn() != null && tbBook.getName() != null && tbBook.getPagesNumber() != null
				&& tbBook.getPress() != null && tbBook.getPrice() != null && tbBook.getPublicationTime() != null
				&& tbBook.getSeries() != null) {
			tbBook.setIsDeleted(null);
			int i = tbBookMapper.updateByPrimaryKeySelective(tbBook);
			if (i == 1) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean addTbBook(TbBook tbBook) {
		if (tbBook.getAuthor() != null && tbBook.getBindingLayout() != null && tbBook.getIsbn() != null
				&& tbBook.getName() != null && tbBook.getPagesNumber() != null && tbBook.getPress() != null
				&& tbBook.getPrice() != null && tbBook.getPublicationTime() != null && tbBook.getSeries() != null) {
			int i = tbBookMapper.insertSelective(tbBook);
			if (i == 1) {
				return true;
			}
		}
		return false;
	}

}
